package com.lock.reentrantLock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @PackageName:com.lock.reentrantLock
 * @ClassName:Test01
 * @Description: lock锁的基本使用 多个线程都调用同一个代码块
 * @Author: zhangxiaoyu
 * @date 2021/1/19 16:01
 */
public class Test01 {

    //定义显示锁
    static Lock lock = new ReentrantLock();

    public static void sm(){
        //获得锁
        lock.lock();

        //同步代码块
        for (int i=0;i<100;i++){
            System.out.println(Thread.currentThread().getName()+"---"+i);
        }
        //释放锁
        lock.unlock();
    }

    public static void main(String[] args){

        Runnable r = new Runnable() {
            @Override
            public void run() {
                sm();
            }
        };

        //启动三个线程
        new Thread(r).start();
        new Thread(r).start();
        new Thread(r).start();
    }
}
